package xin.alum.aim.rocketmq;

import lombok.SneakyThrows;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import xin.alum.aim.cluster.ClusterPusher;
import xin.alum.aim.groups.Session;
import xin.alum.aim.model.Transportable;

public class RocketMQPusher extends ClusterPusher {

    private final RocketMqProducer rocketMqProducer;

    private final Jackson2JsonRedisSerializer<Object> jackson;

    public RocketMQPusher(RocketMqProducer rocketMqProducer,Jackson2JsonRedisSerializer<Object> json) {
        this.rocketMqProducer = rocketMqProducer;
        this.jackson=json;
    }

    @SneakyThrows
    protected <T> boolean channel(String channel, T d) {
        if(d instanceof Transportable) {
            Transportable t=(Transportable)d;
            rocketMqProducer.sendAsync(t.getRecipient(), jackson.serialize(d));
        }else if(d instanceof Session) {
            rocketMqProducer.sendAsync(RocketMQConfig.TAG_KILL, jackson.serialize(d));
        }else{
            logger.warn("通信数据请继承自：Transportable");
        }
        return true;
    }
}
